//
//  TYTextField.m
//  liangyi
//
//  Created by 张水 on 16/12/19.
//  Copyright © 2016年 yswh. All rights reserved.
//

#import "TYTextField.h"

@interface TYTextField ()

@property (nonatomic, strong) UILabel *leftLabel;

@property (nonatomic, strong) UIImageView *leftImageView;

@end

@implementation TYTextField
@dynamic leftTitle;
@dynamic leftTitleColor;
@dynamic leftTitleFont;
@dynamic leftImage;
@dynamic leftStyle;

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
                    leftStyle:(TYTextFieldLeftStyle)leftStyle
{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
        switch (leftStyle) {
            case TYTextFieldStyleLeftTitle:
                [self setupLabel];
                break;
            case TYTextFieldStyleLeftImage:
                [self setupImageView];
                break;
        }
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    }
    return self;
}

- (void)setupLabel
{
    if (!self.leftLabel) {
        UILabel *label = [UILabel new];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor lightGrayColor];
        label.font = self.font;
        label.textAlignment = NSTextAlignmentLeft;
        self.leftLabel = label;
        self.leftView = self.leftLabel;
        self.leftViewMode = UITextFieldViewModeAlways;
    }
}

- (void)setupImageView {
    if (!self.leftImageView) {
        UIImageView *iv = [UIImageView new];
        iv.backgroundColor = [UIColor clearColor];
        iv.contentMode = UIViewContentModeScaleAspectFit;
        self.leftImageView = iv;
        self.leftView = self.leftImageView;
        self.leftViewMode = UITextFieldViewModeAlways;
    }
}

#pragma mark - setter
- (void)setLeftStyle:(TYTextFieldLeftStyle)leftStyle {
    switch (leftStyle) {
        case TYTextFieldStyleLeftTitle:
            [self setupLabel];
            break;
        case TYTextFieldStyleLeftImage:
            [self setupImageView];
            break;
    }
}

- (void)setLeftTitle:(NSString *)leftTitle {
    [self setupLabel];
    _leftLabel.text = [leftTitle copy];
}

- (void)setLeftTitleFont:(UIFont *)leftTitleFont {
    [self setupLabel];
    _leftLabel.font = leftTitleFont;
}

- (void)setLeftTitleColor:(UIColor *)leftTitleColor {
    [self setupLabel];
    _leftLabel.textColor = leftTitleColor;
}

- (void)setLeftImage:(UIImage *)leftImage {
    [self setupImageView];
    _leftImageView.image = leftImage;
}

#pragma mark - super methods
- (CGRect)leftViewRectForBounds:(CGRect)bounds {
    return CGRectMake(10, 0, 70, bounds.size.height);
}

- (CGRect)rightViewRectForBounds:(CGRect)bounds {
    return  CGRectMake(bounds.size.width - 5, 0, 5, bounds.size.height);
}

#pragma mark - instance method
- (void)setupLayerIfYouNeed {
    self.layer.cornerRadius = 8.0;
    self.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2f].CGColor;
    self.layer.borderWidth = 1.0f;
}

@end
